<template>
  <div>
    <div class="search-container">
      <!--      <el-input class="search-wrap" />-->
      <!--      <el-button type="primary" size="middle" icon="search">查询</el-button>-->
      <el-button type="primary" size="middle" icon="add" @click="handleAdd">新增</el-button>
    </div>

    <el-table :data="list" border>
      <el-table-column prop="name" label="业务名称" />
      <el-table-column prop="bizCode" label="编码" />
      <el-table-column prop="msgCodeList" label="关联模板" />
      <el-table-column prop="description" label="说明" />
      <el-table-column align="center" label="操作" width="200px">
        <template slot-scope="scope">
          <el-button size="small" type="primary" @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog v-loading="loading" title="业务配置" :visible.sync="dialogVisible" width="660px">
      <el-form ref="form" :rules="rules" :model="form" label-position="left" label-width="100px">
        <el-form-item label="名称" prop="name"><el-input v-model="form.name" placeholder="业务名称" /></el-form-item>
        <el-form-item label="编码" prop="bizCode"><el-input v-model="form.bizCode" placeholder="业务编码" /></el-form-item>
        <el-form-item label="模板配置" prop="msgCodeList">
          <el-select v-model="form.msgCodeList" style="width: 520px" multiple placeholder="请选择">
            <el-option v-for="item in templateCodeList" :key="item.id" :label="item.name" :value="item.msgType" />
          </el-select>
        </el-form-item>

        <el-form-item label="说明" prop="description"><el-input v-model="form.description" placeholder="说明" /></el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancel()">取 消</el-button>
        <el-button type="primary" @click="handleSave">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getTemplateList, templateConfigList, addTemplateConfig, templateConfigInfo, updateTemplateConfig, deleteTemplateConfig } from '@/api/wxTemplateBiz'

export default {
  name: 'TemplateMsgConfig',
  data() {
    return {
      pageNum: 0,
      pageSize: 10,
      activeName: 'config',
      dialogVisible: false,
      templateCodeList: [],
      loading: false,
      list: [],
      total: 0,
      templateInfo: null,
      isAdd: true,
      rules: {
        name: [
          {
            min: 1,
            max: 20,
            required: true,
            message: '请输入业务名称, 长度在 1 到 20 个字符',
            trigger: 'blur'
          }
        ],
        bizCode: [
          {
            min: 3,
            max: 255,
            required: true,
            message: '请输入业务编码, 长度在 3 到 20 个字符',
            trigger: 'blur'
          }
        ]
      },
      form: {
        name: '',
        bizCode: '',
        msgCodeList: [],
        description: ''
      }
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    defaultForm() {
      return {
        name: '',
        bizCode: '',
        msgCodeList: [],
        description: ''
      }
    },
    getList() {
      templateConfigList().then(res => {
        this.list = res.data
        return res
      })
    },

    handleAdd() {
      this.templateInfo = null
      this.form = this.defaultForm()
      this.dialogVisible = true
      this.isAdd = true
      getTemplateList().then(res => {
        this.templateCodeList = res.data
      })
    },

    handleEditTemplate(id) {
      this.$router.push({ name: 'editTemplateMsg', params: { id: id } })
    },
    handleEdit(id) {
      this.loading = true
      this.form = this.defaultForm()
      getTemplateList().then(res => {
        this.templateCodeList = res.data
      })
      templateConfigInfo(id)
        .then(resp => {
          this.loading = false
          if (resp.code === 0) {
            const data = resp.data
            data.msgCodeList = data.msgCodeList.split(',')
            this.form = data
            this.dialogVisible = true
            this.isAdd = false
          }
        })
        .catch(() => {
          this.loading = false
        })
    },

    handleCancel() {
      this.dialogVisible = false
    },
    handleDelete(id) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteTemplateConfig(id).then(res => {
          if (res.code === 0) {
            this.getList()
            this.$message.success('删除成功')
          } else {
            this.$message.error(res.msg)
          }
        })
      })
    },
    doAdd(data) {
      addTemplateConfig(data)
        .then(resp => {
          this.loading = false
          if (resp.code === 0) {
            this.$message.success('添加成功！')
            this.dialogVisible = false
            this.getList()
          } else {
            this.$message.error(resp.msg)
          }
        })
        .catch(error => {
          this.loading = false
        })
    },

    doSave(data) {
      updateTemplateConfig(data)
        .then(resp => {
          this.loading = false
          if (resp.code === 0) {
            this.$message.success('修改成功！')
            this.dialogVisible = false
            this.getList()
          } else {
            this.$message.error(resp.msg)
          }
        })
        .catch(error => {
          this.loading = false
        })
    },

    handleSave(id) {
      this.loading = true
      this.$refs.form.validate(valid => {
        if (valid) {
          const data = this.form
          data.msgCodeList = data.msgCodeList.join(',')
          if (this.isAdd) {
            this.doAdd(data)
          } else {
            this.doSave(data)
          }
        } else {
          this.loading = false
          this.$message.error('请按要求填写好表格.')
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.search-container {
	margin-top: 20px;
	margin-bottom: 20px;
}

.search-wrap {
	width: 300px;
	margin-right: 10px;
}
::v-deep .el-input__inner:focus {
	border-color: $btnBgc;
}
::v-deep .el-select .el-input.is-focus .el-input__inner{
	border-color: $btnBgc;
}
.el-select-dropdown.is-multiple .el-select-dropdown__item.selected{
	color: $btnBgc;
}
</style>
